<?php  
class ControllerCategoryManger extends Controller {
	public function index() {
		$this->document->setTitle($this->config->get('config_title').'添加目录');
		$this->document->setDescription($this->config->get('config_meta_description'));
		$this->data['heading_title'] = '添加目录';
        
        $this->load->model('category');
        $results = $this->model_category->getCategories();

        foreach ($results as $result) {
            if ($result['status']) {
                $this->data['categories'][] = array(
                    'id'  => $result['cat_id'],
                    'name'  => $result['cat_name'],
                );	
            }
        }
        
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . 'category/add.tpl')) {
			$this->template = $this->config->get('config_template') . 'category/add.tpl';
		} else {
			$this->template = 'category/add.tpl';
		}
		
		$this->children = array(
			'common/footer',
			'common/header'
		);
		$this->response->setOutput($this->render());
	}
    
    public function insert()
    {
        $json = array();
        if (isset($this->request->post['cat_name'])) 
        {
            $cat_name = $this->request->post['cat_name'];
        }
        else
        {
            $cat_name='';
        }
        
        $this->load->model('category');
        $result = $this->model_category->insert($cat_name);
        if($result)
        {
            $json['success'] = '添加目录成功';
        }
        
        $this->load->library('json');
		$this->response->setOutput(Json::encode($json));
    }
    
    public function edit()
    {
		$this->document->setTitle($this->config->get('config_title').'编辑目录');
		$this->document->setDescription($this->config->get('config_meta_description'));
		$this->data['heading_title'] = '编辑目录';
        
        $this->load->model('category');
        if (isset($this->request->get['cat_id'])) 
        {
            $cat_id = $this->request->get['cat_id'];
        }
        else
        {
            $cat_id = 0;
        }
        $this->data['cat_id'] = $cat_id;
        $this->data['cat_name'] = $this->model_category->getCatName($cat_id);

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . 'category/edit.tpl')) {
			$this->template = $this->config->get('config_template') . 'category/edit.tpl';
		} else {
			$this->template = 'category/edit.tpl';
		}
		
		$this->children = array(
			'common/footer',
			'common/header'
		);
		$this->response->setOutput($this->render());
    }
    
    public function update()
    {
        $json = array();
        if (isset($this->request->post['cat_id'])) 
        {
            $cat_id = $this->request->post['cat_id'];
        }
        else
        {
            $cat_id = 0 ;
        }
                
        if (isset($this->request->post['cat_name'])) 
        {
            $cat_name = $this->request->post['cat_name'];
        }
        else
        {
            $cat_name='';
        }
        
        $data = array();
        $data['cat_id'] = $cat_id;
        $data['cat_name'] = $cat_name;
        
        $this->load->model('category');
        $this->model_category->update($data);
        $json['success'] = '目录更新成功';
        //$this->redirect($this->url->link('category/manger', '', 'SSL'));
        $this->load->library('json');
		$this->response->setOutput(Json::encode($json));
        
    }
}
?>